import {createRouter, createWebHashHistory} from 'vue-router'
import {useUserLoginStore} from "@/stores/AccountLogin.js";
import {ElMessage, ElMessageBox} from "element-plus";

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('@/views/HomeView.vue'),
    },

    {
        path: "/enterprise-login",
        name: 'enterprise-login',
        component: () => import("@/views/common/LoginView.vue"),
        meta: {type: 'enterprise'},
    },
    {
        path: "/graduate-login",
        name: 'graduate-login',
        component: () => import("@/views/common/LoginView.vue"),
        meta: {type: 'graduate'},
    },
    {
        path: "/admin-login",
        name: 'admin-login',
        component: () => import("@/views/common/LoginView.vue"),
        meta: {type: 'admin'},
    },
    {
        path: "/register",
        name: 'register',
        component: () => import("@/views/common/RegisterView.vue"),
    },

    /* ----------------------- */

    {
        path: "/enterprise",
        name: 'enterprise-home',
        component: () => import("@/views/enterprise/HomeView.vue"),
        redirect: {name: 'enterprise-select'},
        children: [
            {
                path: "select",
                name: "enterprise-select",
                component: () => import("@/views/enterprise/JobsView.vue"),
            },
            {
                path: "announce",
                name: "enterprise-announce",
                component: () => import("@/views/enterprise/AnnounceView.vue"),
            },
            {
                path: "resume",
                name: "enterprise-resume",
                component: () => import("@/views/enterprise/ResumesView.vue"),
            },
        ],
    },

    /* ----------------------- */

    /* ----------------------- */

    {
        path: "/graduate",
        name: 'graduate-home',
        component: () => import("@/views/graduate/HomeView.vue"),
        redirect: {name: 'graduate-select'},
        children: [
            {
                path: 'select',
                name: 'graduate-select',
                component: () => import("@/views/graduate/JobsView.vue"),
            },
            {
                path: 'putted',
                name: 'graduate-putted',
                component: () => import("@/views/graduate/ResumesView.vue"),
            },
            {
                path: 'resume',
                name: 'graduate-resume',
                component: () => import("@/views/graduate/MyResumeView.vue"),
            },
        ],
    },

    /* ----------------------- */

    {
        path: '/admin',
        name: 'admin-home',
        component: () => import('@/views/admin/HomeView.vue'),
        redirect: {name: 'admin-graduation'},
        children: [
            {
                path: 'graduation',
                name: 'admin-graduation',
                component: () => import('@/views/admin/GraduateView.vue')
            },
            {
                path: 'work',
                name: 'admin-work',
                component: () => import('@/views/admin/WorkView.vue')
            },
            {
                path: 'enterprise',
                name: 'admin-enterprise',
                component: () => import('@/views/admin/EnterpriseView.vue')
            },
            {
                path: 'messages',
                name: 'admin-messages',
                component: () => import('@/views/admin/MessagesView.vue')
            },
        ],
    },

    /* ----------------------- */
    {
        path: '/404',
        name: 'NOT_FOUND',
        component: () => import('@/views/NotFoundView.vue')
    },

    {
        path: '/:pathMatch(.*)*',
        redirect: {name: 'NOT_FOUND'},
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach((to, from) => {
    if (to.name === 'home' || to.name.endsWith('login') || to.name === 'register') return true;
    const isLogin = useUserLoginStore().getIsLogin;
    if (isLogin) return true;
    ElMessage({
        message: '未登录，请登陆',
        type: 'warning',
    })
    return false;
})

export default router
